<div class="users form">
{{if $current_action=='admin_add'}}
<?php 
echo $this->MForm->create($current_model,array(
	'url' => array('action' => 'add'),
));
?>
{{elseif $current_action=='admin_edit'}}
 <?php 
echo $this->MForm->create($current_model,array(
	'class'=>'form-horizontal form',
    'url' => array('action' => 'edit',$id,$copy),
));
?>
{{/if}}
<fieldset>
    	 
            <ul class="nav nav-tabs">
                <li class="active"><a href="#flowstep-main" data-toggle="tab"><span>{{__('Basic Info')}} ()</span></a></li>
                <li><a href="#flowstep-template" data-toggle="tab"><span>{{__('Template')}}</span></a></li>
                {{$this->Layout->getLanguageTabHead($current_model)}}
            </ul>
            <div class="tab-content">
                <div id="flowstep-main" class="tab-pane active">
                	 <?php
                	 	echo $this->MForm->input('id');
						if($current_action=='admin_add'){
				        	echo $this->MForm->input('name',array('size'=>'80','required'=>'required','onblur'=>"generateSlug(this,'#ArticleSlug')"));
						}
						else{
							echo $this->MForm->input('name',array('size'=>'80','required'=>'required'));
						}
						echo $this->MForm->input('slug');
				         echo $this->MForm->input('flow_id',array( 'required'=>'required')); /*,'options'=>$categories */
				         echo $this->MForm->input('flowmodel');
						echo $this->MForm->input('status',array('default'=>1)); 
						 echo $this->MForm->input('allowactions',array( 'div'=>array( 'class'=>' form-inline'),'after'=>'<div class="clearfix">选择相关的操作时，能自动进入列表页，看到有权限操作的数据以及字段信息。
<br/>点开对应的权限操作菜单，先显示数据列表，选择单条数据后可进行相应操作。</div>')); 
					?>
					<div class="form-group has-success">
						<label for="FlowstepOperateFields" class="col-sm-2 control-label">允许操作的字段</label>
						<div class="col-sm-10 controls textarea">
							<div class="alert alert-info">
								<p>条件与允许值中支持的变量替换在FlowStepHookComponent中扩展</p>
								<p>可查看字段与列表字段若不设置时，与允许操作的字段相同。</p>
								<p>设置了固定值的文本内容字段，在新增编辑时将不可修改值，只允许为设置的固定值。</p>
							</div>
							
							<div role="tabpanel">

							  <!-- Nav tabs -->
							  <ul class="nav nav-tabs" role="tablist">
							    <li role="presentation" class="active"><a href="#operate-settings" aria-controls="home" role="tab" data-toggle="tab">Operate Settings</a></li>
							    <li role="presentation"><a href="#view-list-fields" aria-controls="profile" role="tab" data-toggle="tab">View & List Fields</a></li>
							  </ul>
							
							  <!-- Tab panes -->
							  <div class="tab-content">
							    <div role="tabpanel" class="tab-pane active" id="operate-settings">
							    	<table class="table">
										<thead><tr>
											<td><label><input type="checkbox" class="selectAll" data-target=".operate-checkbox" title="{{__('Select All')}}" id="selectAllViewFields"></label></td>
											<td>Operate Fields</td>
											<td>Allow Associate Values</td>
											<td width="240">Allow Select Values</td>
										</tr></thead>
										<tbody id="FlowStepOperateFields">
											<tr><td></td><td></td><td></td></tr>
											<tr><td></td><td></td><td></td></tr>
										</tbody>
									</table>
							    </div>
							    <div role="tabpanel" class="tab-pane" id="view-list-fields">
							    	<h3>List Fields <small><label><input type="checkbox" class="selectAll" data-target=".list-checkbox" title="{{__('Select All')}}">{{__('Select All')}}</label></small></h3>
							    	<div id="FlowStepListFields">
							    	</div>
							    	<h3>View Fields <small><label><input type="checkbox" class="selectAll" data-target=".view-checkbox" title="{{__('Select All')}}">{{__('Select All')}}</label></small></h3>
							    	<div id="FlowStepViewFields">
							    	
							    	</div>
							    </div>
							  </div>
							
							</div>							
						</div>
					</div>
						<div class="alert alert-info">
								<p>“多选、单选、下拉操作设置了可选值”，或者“文本输入字段设置了固定值”，都将作为查询条件来筛选数据，得到可操作的数据列表</p>
						</div>
					<?php	
						echo $this->MForm->input('conditions',array('use_editor'=>false,'rows' => '5',));
						echo $this->MForm->input('content',array('rows' => '4','use_editor'=>false, 'cols' => '80')); 
				     ?>
				     <div class="clearfix"></div>				 
                </div>
                
                 <div id="flowstep-template"  class="tab-pane">
	                 	<?php
					        echo $this->MForm->input('template',array('rows' => '2','use_editor'=>false, 'cols' => '80')); 
					    ?>
                 </div>
                 {{$this->Layout->getLanguageTabContent($current_model)}}          	
       </div>
 
    
    <div class="form-group">
    	<label class="col-sm-2 control-label"></label>
    	<div class="col-sm-10 controls"><?php echo $this->MForm->submit(__('Submit'),array('id'=>'btnsubmit')); ?></div>
    </div>
    </fieldset>
	{{$this->MForm->end()}}
</div>

<script>
function loadModelSchema(model)
{
	if(model !=''){
		ajaxAction(ADMIN_BASEURL+'/admin/flowsteps/loadOperateFields/'+model+'/{{$id}}','',null,function(request) {
			
			var extschema = request.extschema;
			var field_settings = request.field_settings;
			
			var currentModelTo = $('#FlowstepFlowmodel').val();
			var ohtml= lhtml = vhtml = "";
			$('#FlowStepOperateFields').empty();
			
			//alert(request.field_settings);
			
			for(var i in field_settings) {
				var view_checked = operate_checked = list_checked = '';
				
				if(request.view_fields && $.inArray(i,request.view_fields) >=0) {
					view_checked = ' checked="checked" ';
				}
				if(request.operate_fields && $.inArray(i,request.operate_fields) >=0) {
					operate_checked = ' checked="checked" ';
				}
				if(request.list_fields && $.inArray(i,request.list_fields) >=0) {
					list_checked = ' checked="checked" ';
				}
				
				lhtml += '<label><input type="checkbox" '+list_checked+' name="data[OperateFields][list_fields][]" value="'+i+'" class="list-checkbox">'+extschema[i]['translate']+' ('+i+')'+'</label>';
				vhtml += '<label><input type="checkbox" '+view_checked+' name="data[OperateFields][view_fields][]" value="'+i+'" class="view-checkbox">'+extschema[i]['translate']+' ('+i+')'+'</label>';;
				
				if(extschema[i]['formtype'] == 'select' || extschema[i]['formtype'] == 'radio' || extschema[i]['formtype'] == 'checkbox' ){
					var selected_values = allow_checked = '';
					for(var v in extschema[i]['selectvalues']){
						allow_checked = '';
						if(extschema[i].allow_values && $.inArray(v,extschema[i].allow_values) >= 0) {
							allow_checked = ' checked="checked" ';
						}						
						selected_values += '<label><input type="checkbox" '+allow_checked+' name="data[OperateFields][field_settings]['+i+'][allow_values][]" value="'+v+'">' + extschema[i]['selectvalues'][v] + '</label><br/>';
					}
					
					//<textarea cols="60" rows="7" class="form-control" placeholder="{{__("Allow Selected Values")}}" name="data[OperateFields][field_settings]['+i+'][allow_values]">
					
					ohtml+='<tr><td><input type="checkbox" '+operate_checked+' name="data[OperateFields][operate_fields][]" value="'+i+'" class="operate-checkbox" > </td><td>'+extschema[i]['translate']+'<br/>('+i+')'+'</td><td><textarea cols="60" rows="7" class="form-control" placeholder="{{__("Allow Associate Conditions")}}" name="data[OperateFields][field_settings]['+i+'][associate]">'+extschema[i]['associate']+'</textarea></td><td>'+selected_values+'</td></tr>';
				}
				else{
					var allow_values = extschema[i]['allow_values']? extschema[i]['allow_values'] : '';
					ohtml+='<tr><td><input type="checkbox" '+operate_checked+' name="data[OperateFields][operate_fields][]" value="'+i+'" class="operate-checkbox"> </td><td>'+extschema[i]['translate']+'<br/>('+i+')'+'</td><td colspan="2"><input type="text" class="form-control" placeholder="{{__("Specified Value. It will fixed to this value.")}}" name="data[OperateFields][field_settings]['+i+'][allow_values]" value="'+allow_values+'" ></td></tr>';
				}
				
				
			}
			
			$('#FlowStepOperateFields').append(ohtml);
			$('#FlowStepListFields').append(lhtml);
			$('#FlowStepViewFields').append(vhtml);
			
			$('#FlowStepOperateFields').sortable({
				revert:true,
				cancel:':input,:radio,:image,:button',
				///update:function(event,ui){
					//console.log($(this).sortable('toArray'));serialize
					//ajaxAction(ADMIN_BASEURL+'/admin/settings/sort.json',$(".settings-sortable").sortable( "serialize" ));
				//}
			});
			
			
			
		});
	}
}

	$(document).ready(function(){
		
		 $(".selectAll").click(function(){   
			    if(this.checked){   
			        $("input[type='checkbox']"+$(this).data('target'),this.form).each(function(){this.checked=true;});
			    }else{   
			        $("input[type='checkbox']"+$(this).data('target'),this.form).each(function(){this.checked=false;});   
			    }
		 });
		 
			    
		currentModelTo = $('#FlowstepFlowmodel').val();
		$('#FlowstepFlowmodel').change(function(){
			$('#FlowStepOperateFields').empty();
			loadModelSchema(this.value);
		});
		loadModelSchema(currentModelTo);
	});
</script>
